package com.example.myapplication;


import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Environment;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private List<Video> PersonList;
    int wh ;

    public RecyclerAdapter(List<Video> PersonList,int wh)
    {
        this.PersonList = PersonList;
        this.wh = wh;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);

        ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    };

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {


       // holder.id.setText(String.valueOf(Formatter.formatFileSize(PersonList.get(position).getSize())));
        holder.name.setText(PersonList.get(position).getName());
        //holder.action.setText(PersonList.get(position).getAction());
        holder.videoView.setVideoURI(Uri.parse(PersonList.get(position).getPath()));

//        String path  = Environment.getExternalStorageDirectory().getPath();
//        MediaMetadataRetriever media = new MediaMetadataRetriever();
//        media.setDataSource(path+"/123.rmvb");
//        Bitmap bitmap = media.getFrameAtTime();
       // holder.imageView.setImageBitmap(bitmap);
       // holder.imageView.setImageURI(Uri.parse(PersonList.get(position).getPath()));

//        Glide.with(holder.imageView.getContext()).setDefaultRequestOptions(
//                new RequestOptions()
//                .frame(0)
//                .centerCrop()
//        ).load(Uri.parse(PersonList.get(position).getPath()))
//                .into(holder.imageView);

        Glide.with(holder.imageView.getContext())
                .load(PersonList.get(position).getPath())
                .into(holder.imageView);
      //  holder.imageView.setImageBitmap(PersonList.get(position).getBitmap());
        holder.imageView.setLayoutParams(new RelativeLayout.LayoutParams(wh,wh));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                   listener.onClick(position);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return PersonList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView id;
        TextView name;
        TextView action;
        VideoView videoView;
        ImageView imageView;
        RelativeLayout rv;

        public ViewHolder(@NonNull View itemView)
        {

            super(itemView);
            this.rv= itemView.findViewById(R.id.rvl);
            this.id= itemView.findViewById(R.id.tv_duration);
            this.name= itemView.findViewById(R.id.tv_name);
            this.action= itemView.findViewById(R.id.tv_size);
            this.videoView= itemView.findViewById(R.id.vv);
            this.imageView= itemView.findViewById(R.id.iv);
        }

    }


    //第一步 定义接口
    public interface OnItemClickListener {
        void onClick(int position);
    }

    private OnItemClickListener listener;

    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
}